const router = require("express").Router();

const students = [
  { id: 1, name: "John", age: 18 },
  { id: 2, name: "Jane", age: 19 },
  { id: 3, name: "Bob", age: 20 },
];

router.get("/students", (req, res) => {
  res.setHeader("Content-Type", "application/json");
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.send(students);
});

router.post("/addstudent", (req, res) => {
    console.log("JJJJ");
    
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.send("添加成功"); 
});

// 这里需要做个预检请求
router.options("/addstudent",(req,res)=>{
    console.log("预检请求");
    
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Headers", "*");
    res.setHeader("Access-Control-Allow-Methods", "*");
    res.send();
})


router.get("/data",(req,res)=>{
  // 用于服务器之间的数据访问
  res.setHeader("Content-Type", "application/json");
  res.send({
    name: "张三",
    age: 18
  })
})




module.exports = router;
